package com.chengcang.api.modules.busi.controller;


import com.chengcang.api.common.annotation.Login;
import com.chengcang.api.modules.busi.dto.BusiAddressDTO;
import com.chengcang.api.modules.busi.dto.BusiAddressDeleteDTO;
import com.chengcang.api.modules.busi.dto.BusiAddressPageDTO;
import com.chengcang.api.modules.busi.service.BusiAddressService;
import com.chengcang.api.modules.busi.vo.BusiAddressVo;
import com.chengcang.common.exception.CustomException;
import com.chengcang.common.page.PageData;
import com.chengcang.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.regex.Pattern;


/**
 * 地区表
 *
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地区表")
public class ApiAddressController {
    @Autowired
    private BusiAddressService busiAddressService;

    @Login
    @GetMapping("page")
    @ApiOperation("分页")
    public Result<PageData<BusiAddressVo>> page(BusiAddressPageDTO requestDto) {
        PageData<BusiAddressVo> page = busiAddressService.page(requestDto);
        return new Result<PageData<BusiAddressVo>>().ok(page);
    }


    @PostMapping
    @ApiOperation("保存")
    @Login
    public Result<?> save(@RequestBody BusiAddressDTO dto){
        busiAddressService.saveAddr(dto);
        return new Result<Object>();
    }

    @PostMapping("setDefault")
    @ApiOperation("设置默认地址")
    @Login
    public Result<?> setDefault(String id){
        busiAddressService.setDefault(id);
        return new Result<Object>();
    }

    @PutMapping
    @ApiOperation("修改")
    @Login
    public Result update(@RequestBody BusiAddressDTO dto){
        if (!Pattern.matches("^[0-9-]{13}$",dto.getAddressMobile())){
            throw new CustomException("电话格式错误");
        }
        busiAddressService.modify(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @Login
    public Result<?> delete(@RequestBody BusiAddressDeleteDTO dto){

        busiAddressService.delete(dto.getIds());
        return new Result<Object>();
    }
}